###################################################
#           Replication file for                  #
# Thürk and Klüver: Hitting the Sweet Spot? (CPS) #
# Additional evidence for causal mechanisms       #
#            Figure 4 and Figure 5                #
#          Figures 12 and 13 Appendix             #
###################################################

# Clean the workspace
rm(list = ls())

# Load necessary libraries
library(tidyverse)
library(ggplot2)
library(conflicted)
library(dplyr)
library(readxl)

# Set working directory (please update this to the appropriate path)
setwd("")

# Load dataset
load("data_fig4.rda")

################
### Figure 4 ###
################

# Calculate percentages for cases
case_pool <- d_all %>% 
  group_by(country, year, government) %>% 
  summarise(per = sum(pol_info, na.rm = TRUE) / length(pol_info[!is.na(pol_info)]))

case_pool <- case_pool %>% 
  mutate(case = paste(country, year, sep = " "))

# Pool percentages
case_pool_values <- case_pool %>% 
  group_by(government) %>% 
  summarise(per = sum(per) / length(per))

# Add pooled percentages to the dataset
case_pool <- case_pool %>% 
  ungroup() %>% 
  add_case(country = "pooled", year = 0000, government = "Majority", per = 0.7304607, case = "pooled:Majority") %>% 
  add_case(country = "pooled", year = 0000, government = "Minority", per = 0.3609437, case = "pooled:Minority")

# Order cases so that pooled values are at the end
case_pool$case <- factor(case_pool$case, levels = rev(c("Denmark 2007", "Italy 1990", "New Zealand 2002", 
                                                        "New Zealand 2005", "New Zealand 2008", "New Zealand 2014", 
                                                        "New Zealand 2017", "Norway 1965", "Norway 2001", "Norway 2009", 
                                                        "Sweden 2002", "Sweden 2006", "Sweden 2010", 
                                                        "pooled:Majority", "pooled:Minority")), ordered = TRUE)

# Plot Figure 4
Figure4 <- ggplot(case_pool, mapping = aes(x = per, y = case)) +
  geom_bar(stat = "identity", aes(fill = government)) +
  scale_x_continuous(limits = c(0, 1), labels = scales::percent) +
  ylab("Cases") +
  xlab("Percentage of correct answers") +
  labs(title = "Percentage of respondents correctly identifying parties in government") +
  theme(plot.title = element_text(hjust = 0.5))
# Display the graph
Figure4

################
### Figure 5 ###
################

# Specify the path to the Excel file (please update this to the appropriate path)
file_pathfig5 <- ".../data_fig5.xlsx"

# Load the Excel data
datafig5 <- read_excel(file_pathfig5)

# Plot Figure 5 for Center Party
Figure5 <- ggplot(data = datafig5, aes(x = year, y = C, ymin = C_low, ymax = C_high, fill = type, linetype = type)) + 
  geom_line(color = "black") +
  geom_point(shape = 1, color = "black", size = 1) +
  geom_ribbon(alpha = 0.1) + 
  scale_fill_manual(values = c("#4393C3", "red")) +
  geom_vline(xintercept = 2004.5, linetype = "dashed", color = "red") +
  geom_vline(xintercept = 2018.5, linetype = "dashed", color = "red") +
  xlab("Year") + 
  ylab("Popularity Center Party") +
  ggtitle("Public Assessment of Center Party") + 
  theme(legend.position = "bottom", legend.title = element_blank())

# Display the graph
Figure5


#################
### Figure 12 ###
#################

# Plot Figure 12 for Christian Democrats
Figure12 <- ggplot(data = datafig5, aes(x = year, y = KD, ymin = KD_low, ymax = KD_high, fill = type, linetype = type)) + 
  geom_line(color = "black") +
  geom_point(shape = 1, color = "black", size = 1) +
  geom_ribbon(alpha = 0.1) + 
  scale_fill_manual(values = c("#4393C3", "red")) +
  geom_vline(xintercept = 2004.5, linetype = "dashed", color = "red") +
  geom_vline(xintercept = 2018.5, linetype = "dashed", color = "red") +
  xlab("Year") + 
  ylab("Popularity Christian Democrats") +
  ggtitle("Public Assessment of Christian Democrats") + 
  theme(legend.position = "bottom", legend.title = element_blank())

# Display the graph
Figure12

#################
### Figure 13 ###
#################

# Plot Figure 13 for Liberal Party
Figure13 <- ggplot(data = datafig5, aes(x = year, y = FP, ymin = FP_low, ymax = FP_high, fill = type, linetype = type)) + 
  geom_line(color = "black") +
  geom_point(shape = 1, color = "black", size = 1) +
  geom_ribbon(alpha = 0.1) + 
  scale_fill_manual(values = c("#4393C3", "red")) +
  geom_vline(xintercept = 2004.5, linetype = "dashed", color = "red") +
  geom_vline(xintercept = 2018.5, linetype = "dashed", color = "red") +
  xlab("Year") + 
  ylab("Popularity Liberal Party") +
  ggtitle("Public Assessment of Liberal Party") + 
  theme(legend.position = "bottom", legend.title = element_blank())

# Display the graph
Figure13

